package com.dora.http.base.headerfilter;

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import okio.Buffer;
import okio.BufferedSource;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;

/**
 * @Author: Dora
 * @Description: 打印请求响应
 * @Date: 2022/1/4
 */
@Slf4j
public class RequestResOkLogInterceptor implements Interceptor {
    private static final Charset UTF8 = Charset.forName("UTF-8");

    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {

        //打印request报文
        Request request = chain.request();
        RequestBody requestBody = request.body();
        String reqBody = null;
        if (requestBody != null){
            Buffer buffer = new Buffer();
            requestBody.writeTo(buffer);
            
            Charset charset = UTF8;
            MediaType mediaType = requestBody.contentType();
            if (mediaType != null){
                charset = mediaType.charset(UTF8);
            }
            reqBody = buffer.readString(charset);
        }
        log.info("\n#################################### Request ALL ####################################\n" +
                "|### → URL : " + request.url() + "\n" +
                "|### → HTTP_METHOD : " + request.method() + "\n" +
                "|### → HTTP_HEADER : " + request.headers() + "\n" +
                "|### → RequestBody : {" + reqBody + "}\n" +
                "################################################################# Request Done #################################################################\n" );

        //打印Response报文
        //先执行请求，才能获取报文
        Response response = chain.proceed(request);
        ResponseBody responseBody = response.body();
        String respBody = null;
        if(responseBody != null) {
            BufferedSource source = responseBody.source();
            source.request(Long.MAX_VALUE);
            Buffer buffer = source.buffer();

            Charset charset = UTF8;
            MediaType mediaType = responseBody.contentType();
            if (mediaType != null) {
                try {
                    charset = mediaType.charset(UTF8);
                } catch (UnsupportedCharsetException e) {
                    e.printStackTrace();
                }
            }
            respBody = buffer.clone().readString(charset);
        }
        log.info("\n#################################### Response ALL ####################################\n" +
                "|### → rCode : " + response.code() + "\n" +
                "|### → ResponseBody : " + respBody + "\n" +
                "|### → ResponseMsg : {" + response.message() + "}\n" +
                "################################################################# Response Done #################################################################\n" );

        return response;
    }
}
